cmake_minimum_required(VERSION 3.0)

project(p4c-pc)

if ("${CMAKE_BUILD_TYPE}" STREQUAL "DEBUG")
add_definitions(-DDEBUG -D_DEBUG)
endif()

set (src
    any.h
    any.cpp
    ast.h
    ast.cpp
    buffer.h
    base.h
    base.c
    common.h
    common.c
    debug.cpp
    decl.h
    decl.cpp
    expr.h
    expr.cpp
    gc.h
    gc.cpp
    gen.h
    gen.cpp
    intrinsic.h
    intrinsic.cpp
    module.h
    module.cpp
    option.h
    option.c
    stmt.h
    stmt.cpp
    symbol.h
    symbol.cpp
    token.h
    token.cpp
    type.h
    type.cpp
)

add_definitions("-D_CRT_SECURE_NO_WARNINGS")

add_executable(qcc main.cpp ${src})

if (CMAKE_HOST_WIN32)
add_custom_target(finish
    COMMAND echo "start:" > ..\\log.txt
    COMMAND .\\qcc ../case.p4 --dump-token --token-only > ..\\log.txt & echo error count: %ERRORLEVEL% >> ..\\log.txt
    COMMAND .\\qcc ../case2.txt --dump-ast --dump-expr --dump-ref >> ..\\log.txt
    COMMAND .\\qcc ../case2.txt --dump-token --token-only > ..\\case2.token.txt
    COMMAND echo "---------------------------------------" >> ..\\log.txt
    COMMAND ./qcc ../case2.txt >> ..\\log.txt
    COMMAND dir ..\\log.txt
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
    COMMENT "finish at ${CMAKE_CURRENT_BINARY_DIR}"
)
elseif (CMAKE_HOST_UNIX)
add_custom_target(finish
    COMMAND echo "start:" > ../log.txt
    COMMAND ./qcc ../case.p4 --dump-token --token-only > ../log.txt || echo error count: $$? >> ../log.txt && echo \"error count: $$?\" >> ../log.txt
    COMMAND ./qcc ../case2.txt --dump-ast --dump-expr --dump-ref >> ../log.txt
    COMMAND echo "---------------------------------------" >> ../log.txt
    COMMAND ./qcc ../case2.txt >> ../log.txt
    COMMAND ls -l ../log.txt
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
    COMMENT "finish at ${CMAKE_CURRENT_BINARY_DIR}"
)
endif()

if (CMAKE_HOST_WIN32)
add_custom_target(tests
    COMMAND echo "dir: ${CMAKE_SOURCE_DIR}/case"
    COMMAND echo "bin: ${CMAKE_CURRENT_BINARY_DIR}/case"
    COMMAND .\\test.bat ${CMAKE_CURRENT_BINARY_DIR}\\qcc
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}\\case
    COMMENT "tests"
)
elseif (CMAKE_HOST_UNIX)
add_custom_target(tests
    COMMAND echo "dir: ${CMAKE_SOURCE_DIR}/case"
    COMMAND echo "bin: ${CMAKE_CURRENT_BINARY_DIR}/case"
    COMMAND bash test.sh ${CMAKE_CURRENT_BINARY_DIR}/qcc
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/case
    COMMENT "tests"
)
endif()

add_dependencies(finish qcc)

add_executable(test_common common.h common.c test_common.c)

add_dependencies(tests qcc)